package com.fan.shop.views.bean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.ser.FilterProvider;
import org.codehaus.jackson.map.ser.impl.SimpleBeanPropertyFilter;
import org.codehaus.jackson.map.ser.impl.SimpleFilterProvider;

import com.fan.shop.global.JacksonMapper;
import com.fan.shop.model.commonbrowser.CommonBrowser;

@ManagedBean(name = "commonBrowserBean")
@ViewScoped
public class CommonBrowserBean extends ShopBean{

	private static Logger logger = Logger.getLogger(CommonBrowserBean.class);

	private CommonBrowser commonBrowser;

	private String tableData;
	private int columnsSize;
	private List<String> columnNames;
	private String selectionMode = "single";

	private String selectedValue;

	public CommonBrowser getCommonBrowser() {
		return commonBrowser;
	}

	public void setCommonBrowser(CommonBrowser commonBrowser) {
		this.commonBrowser = commonBrowser;
	}

	public int getColumnsSize() {
		columnsSize = commonBrowser.getShopService().getColumnsSize();
		return columnsSize;
	}

	public void setColumnsSize(int columnsSize) {
		this.columnsSize = columnsSize;
	}

	public List<String> getColumnNames() {
		columnNames = commonBrowser.getShopService().getColumnNames();
		return columnNames;
	}

	public void setColumnNames(List<String> columnNames) {
		this.columnNames = columnNames;
	}

	public String getTableData() {
		ObjectMapper mapper = JacksonMapper.getInstance();

		Set<String> columnNames = new HashSet<String>();
		for (String name : commonBrowser.getShopService().getColumnNames()) {
			columnNames.add(name);

		}
		FilterProvider filters = new SimpleFilterProvider().addFilter(
				"myFilter",
				SimpleBeanPropertyFilter.filterOutAllExcept(columnNames));

		tableData = "\"aaData\": [";
		StringBuffer data = new StringBuffer();

		for (Object obj : commonBrowser.getShopService().getList()) {
			try {
				data = data.append(mapper.writer(filters).writeValueAsString(
						obj));

			} catch (JsonGenerationException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String tmp = data.toString().replaceAll("\\{[^,:\\{\\}]*:", "[");
		tmp = tmp.replaceAll(",[^,:\\{\\}]*:", ",");

		tableData = tableData + tmp.replace("{", "[").replace("}", "],");
		if (tableData.endsWith(",")) {
			tableData = tableData.substring(0, tableData.length() - 1);
		}

		tableData = tableData + "]";

		return tableData;
	}

	public void setTableData(String tableData) {
		this.tableData = tableData;
	}

	public String getSelectionMode() {
		selectionMode = commonBrowser.getSelectionMode();
		return selectionMode;
	}

	public void setSelectionMode(String selectionMode) {
		this.selectionMode = selectionMode;
	}

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		CommonBrowserBean.logger = logger;
	}

	public String getSelectedValue() {
		return selectedValue;
	}

	public void setSelectedValue(String selectedValue) {
		this.selectedValue = selectedValue;
	}

	public CommonBrowserBean() {

		Map<String, Object> map = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestMap();
		if (map.containsKey("common_browser")) {
			commonBrowser = (CommonBrowser) map.get("common_browser");
		}

	}

	public String returnAction() {

		List<String> list = getReturnList(selectedValue);
		commonBrowser.setReturnList(list);

		FacesContext.getCurrentInstance().getExternalContext().getRequestMap()
				.put("common_browser", commonBrowser);

		return commonBrowser.getParentPage();
	}
	
	public String backAction()
	{
		List<String> list = new ArrayList<String>();
		commonBrowser.setReturnList(list);

		FacesContext.getCurrentInstance().getExternalContext().getRequestMap()
				.put("common_browser", commonBrowser);
		return commonBrowser.getParentPage();
	}

	public List<String> getReturnList(String values) {
		List<String> list = new ArrayList<String>();
		String[] valuesArray = values.split(";");
		for (String value : valuesArray) {
			list.add(value);
		}
		return list;
	}


	public void clearFields() {

	}

}
